package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.LoginAnnotation;
import cn.wolfcode.wolf2w.annotation.UserParams;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyVORedisService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 甘静斌 on 2020-08-13 23:08
 */
@RestController
@RequestMapping("/strategies")
public class StrategyController {


    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyVORedisService strategyVORedisService;
    /**
     * 攻略查看明细
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Object detail(String id){

        Strategy strategy = strategyService.get(id);

        //阅读数 +1
        strategyVORedisService.viewIncreates(id,1);

        return JsonResult.success(strategy);
    }

    /**
     * 攻略查看所有
     * @param qo
     * @return
     */
    @GetMapping("/query")
    public Object query(StrategyQuery qo){


        Page<Strategy> page = strategyService.query(qo);
        return JsonResult.success(page);
    }

    /**
     * 全部主题显示
     * @return
     */
    @GetMapping("/themes")
    public Object themes(){


        List<StrategyTheme> list = strategyThemeService.list();
        return JsonResult.success(list);
    }

    /**
     * 添加 评论 评论数 + 1
     * @param strategyComment
     * @param userInfo
     * @return
     */
    @LoginAnnotation
    @PostMapping("/addComment")
    public Object addComment(StrategyComment strategyComment, @UserParams UserInfo userInfo){
        //userInfo是源,strategyComment是目标,strategyComment里面没有id
        BeanUtils.copyProperties(userInfo,strategyComment);
        //获取源的id
        strategyComment.setUserId(userInfo.getId());
        //把源的id设置进攻略评论表中
        strategyCommentService.save(strategyComment);

        //评论数 + 1
        strategyVORedisService.replynumIncreates(strategyComment.getStrategyId(),1);

        return JsonResult.success();
    }


    /**
     * 攻略评论分页
     * @param qo
     * @return
     */
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){

        Page<StrategyComment> page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }

    /**
     * 给攻略评论点赞
     * @param cid
     * @param userInfo
     * @return
     */
    @LoginAnnotation
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid,@UserParams UserInfo userInfo){


       strategyCommentService.commentThumb(cid,userInfo);

        return JsonResult.success();
    }

    /**
     * 阅读数 + 1
     * @param sid
     * @return
     */
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        //通过攻略sid获取到vo对象,返回给页面
        return JsonResult.success(strategyVORedisService.getStatisVo(sid));
    }

    /**
     * 收藏数 + 1
     * @param sid
     * @param userInfo
     * @return
     */
    @LoginAnnotation
    @PostMapping("/favor")
    public Object favor(String sid,@UserParams UserInfo userInfo){

        boolean f = strategyVORedisService.favorIncreaces(sid,userInfo);

        return JsonResult.success(f);
    }

    /**
     * 点赞数 + 1
     * @param sid
     * @param userInfo
     * @return
     */
    @LoginAnnotation
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParams UserInfo userInfo){

        boolean th = strategyVORedisService.strategyThumbup(sid,userInfo.getId());

        return JsonResult.success(th);
    }



}
